

import 'package:flutter/material.dart';

class GridViewDemo extends StatelessWidget {
  const GridViewDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 350,
      padding: EdgeInsets.all(10),
      child: GridView(
        gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
          maxCrossAxisExtent: 180, /* 子组件最大宽度，通过设定宽度已自适应容器组件布局 */
          mainAxisSpacing: 10, /* 主轴间距值 */
          crossAxisSpacing: 10, /* 交叉轴间距 */
          childAspectRatio: 1.5, /* 子组件的宽高比例 */
        ),
        children: [
          Container(color: Colors.blue,),
          Container(color: Colors.green,),
          Container(color: Colors.red,),
          Container(color: Colors.yellow,),
          Container(color: Colors.purple,),
          Container(color: Colors.blueAccent,),
          Container(color: Colors.greenAccent,),
          Container(color: Colors.redAccent,),
          Container(color: Colors.yellowAccent,),
          Container(color: Colors.purpleAccent,),
        ],
      ),

      // child: GridView(
      //   gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
      //     crossAxisCount: 2, /*  指定列数 */
      //     mainAxisSpacing: 10, /* 主轴间距值 */
      //     crossAxisSpacing: 10, /* 交叉轴间距 */
      //     childAspectRatio: 1.5, /* 子组件的宽高比例 */
      //   ),
      //   children: [
      //     Container(color: Colors.blue,),
      //     Container(color: Colors.green,),
      //     Container(color: Colors.red,),
      //     Container(color: Colors.yellow,),
      //     Container(color: Colors.purple,),
      //     Container(color: Colors.blueAccent,),
      //     Container(color: Colors.greenAccent,),
      //     Container(color: Colors.redAccent,),
      //     Container(color: Colors.yellowAccent,),
      //     Container(color: Colors.purpleAccent,),
      //   ],
      // ),
    );
  }
}

class GridViewCountDemo extends StatelessWidget {
  const GridViewCountDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 350,
      padding: EdgeInsets.all(10),
      child: GridView.count(
        crossAxisCount: 2,
        crossAxisSpacing: 10,
        childAspectRatio: 1.6,
        mainAxisSpacing: 10,
        physics: const BouncingScrollPhysics(), // 弹性滚动边界
        // physics: const ClampingScrollPhysics(), // 固定滚动边界
        // physics: const AlwaysScrollableScrollPhysics(), // 滚动
        // physics: const NeverScrollableScrollPhysics(), // 禁止滚动
        children: List.generate(30, (idx) => Image.asset('images/stratos-0.jpg')),
      ),
    );
  }

}